package com.example.servlet.request;

import com.example.bean.Student1;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @program: sms
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-02 14:57
 **/
@WebServlet("/demo03")
public class RequestDemo03 extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(RequestDemo03.class);

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // test(req, resp);
        // try {
        //     test2(req, resp);
        // } catch (InvocationTargetException e) {
        //     e.printStackTrace();
        // } catch (IllegalAccessException e) {
        //     e.printStackTrace();
        // }
        test4(req, resp);
        // try {
        //     test3(req, resp);
        // } catch (Exception e) {
        //     e.printStackTrace();
        // }
    }

    private void test3(HttpServletRequest req, HttpServletResponse resp) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobby");
        Class<?> studentClass = Class.forName("com.example.bean.Student1");
        Student1 student1 = (Student1) studentClass.getConstructor(
                String.class, String.class, String[].class)
                .newInstance(username, password, hobbies);
        System.out.println(student1.toString());

        Class<Student1> sc1 = Student1.class;
        Student1 student = sc1.getConstructor().newInstance();
        Method usernameMethod = sc1.getMethod("setUsername", String.class);
        usernameMethod.invoke(student, username);
        Method passwordMethod = sc1.getMethod("setPassword", String.class);
        passwordMethod.invoke(student, password);
        Method hobbyMethod = sc1.getMethod("setHobby", String[].class);
        System.out.println("test31");
        hobbyMethod.invoke(student, (Object) hobbies);
        System.out.println(student.toString());
        System.out.println("test32");
        System.out.println();
    }

    private void test4(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("test41");
        //通过PropertyDescriptor ,谁的属性描述器,第一个参数决定的,javabean的字节码
        Student1 student = new Student1();
        Map<String, String[]> parameterMap = req.getParameterMap();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            String name = entry.getKey();
            String[] value = entry.getValue();
            try {
                PropertyDescriptor descriptor = new PropertyDescriptor(name, Student1.class);
                Method writeMethod = descriptor.getWriteMethod();
                if (value.length > 1) {
                    writeMethod.invoke(student, (Object) value);
                } else {
                    writeMethod.invoke(student, value);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(student.toString());
        System.out.println("test42");
    }

    private void test5(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void test2(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException {
        Student1 student = new Student1();
        BeanUtils.populate(student, req.getParameterMap());
        System.out.println(student.toString());
        System.out.println("test2");
    }

    private void test(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobby");
        Student1 student = new Student1(username, password, hobbies);
        System.out.println(student.toString());
        System.out.println("test1");
    }

}
